#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-07-28 7:23 下午
# @Author  : yangdy
# @File    : 设计链表
# @Software: PyCharmCE


class MyLinkedList:
    class Node:
        def __init__(self, x):
            self.val = x
            self.next = None

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = None
        self.tail = None
        self.len = 0

    def get(self, index: int) -> int:
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        """
        p = self.head
        while p and index > 0:
            p = p.next
            index -= 1
        if index > 0:
            return -1
        if p:
            return p.val
        return -1

    def addAtHead(self, val: int) -> None:
        """
        Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
        """
        p = self.Node(val)
        if not self.head:
            self.tail = p
        else:
            p.next = self.head
        self.head = p
        self.len += 1

    def addAtTail(self, val: int) -> None:
        """
        Append a node of value val to the last element of the linked list.
        """
        self.display()
        print(f'{self.head}-{self.head.next}', f'{self.tail}-{self.tail.next}', self.len)
        p = self.Node(val)
        if not self.tail:
            self.head = p
        else:
            self.tail.next = p
        self.tail = p
        self.len += 1
        print(f'{self.head}-{self.head.next}', f'{self.tail}-{self.tail.next}', self.len)
        self.display()

    def addAtIndex(self, index: int, val: int) -> None:
        """
        Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
        """
        if index > self.len:
            return
        elif index == self.len:
            self.addAtTail(val)
        else:
            p = self.head
            while p and index > 0:
                p = p.next
                index -= 1
            if index > 0 or not p:
                return
            q = self.Node(p.val)
            q.next = p.next
            p.val = val
            p.next = q
            self.len += 1

    def deleteAtIndex(self, index: int) -> None:
        """
        Delete the index-th node in the linked list, if the index is valid.
        """
        if index > self.len:
            return
        p, q = None, self.head
        while index > 0 and q:
            p, q, index = q, q.next, index-1
        if not q:
            return
        elif not p:
            self.head = q.next
            if not q.next:
                self.tail = q.next
        else:
            p.next = q.next
            if not q.next:
                self.tail = p
        del q
        self.len -= 1

    def display(self):
        print(f'[{self.len}]:', end='')
        p = self.head
        while p:
            print(p.val, end='')
            if p.next:
                print('->', end='')
            p = p.next
        print()

# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)

print('init:')
linkedList = MyLinkedList()
linkedList.display()
print('add head 1:')
linkedList.addAtHead(1)
linkedList.display()
print('add tail 3:')
linkedList.addAtTail(3)
linkedList.display()
print('add index 1 2')
linkedList.addAtIndex(1, 2)    # 链表变为1-> 2-> 3
linkedList.display()
print('get 1:')
print(linkedList.get(1))             # 返回2
linkedList.display()
print('delete 1:')
linkedList.deleteAtIndex(1)   # 现在链表是1-> 3
linkedList.display()
print('get 1')
print(linkedList.get(1))             # 返回3
linkedList.display()
print('delete 0:')
linkedList.deleteAtIndex(0)
linkedList.display()
print('---')
print('add tail 4:')
linkedList.addAtTail(4)
linkedList.display()
print('add tail 3:')
linkedList.addAtTail(3)
linkedList.display()
print('delete 10:')
linkedList.deleteAtIndex(10)
linkedList.display()
